package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.menum.AliProperties;
import com.cl.short_video_java.pojo.AlipayConfig;
import com.cl.short_video_java.pojo.SysSetting;
import com.cl.short_video_java.service.AlipayConfigService;
import com.cl.short_video_java.service.SysSettingService;
import com.cl.short_video_java.mapper.SysSettingMapper;
import com.cl.short_video_java.vo.setting.Alipay;
import com.cl.short_video_java.vo.sys.Setting;
import com.cl.short_video_java.vo.sys.SettingSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author object
 * @description 针对表【sys_setting】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class SysSettingServiceImpl extends ServiceImpl<SysSettingMapper, SysSetting>
		implements SysSettingService {

	@Autowired
	private AlipayConfigService configService;

	@Override
	public String getSysSettingByKey(String key) {
		SysSetting one = this.getOne(new QueryWrapper<SysSetting>().eq("`key`", key));
		return one.getValue() == null ? "" : one.getValue();
	}

	/**
	 * 根据条件获取设置参数列表
	 *
	 * @param settingSearch 搜索条件
	 * @return 查询结果
	 */
	@Override
	public List<SysSetting> search(SettingSearch settingSearch) {

		List<SysSetting> list = this.list(new QueryWrapper<SysSetting>()
				.like(settingSearch.getKey() != null, "`key`", settingSearch.getKey())
				.eq(settingSearch.getType() != 2, "`type`", settingSearch.getType())
				.eq(settingSearch.getStatus() != 2, "`status`", settingSearch.getStatus())
				.like(settingSearch.getNote() != null, "`note`", settingSearch.getNote())
		);
		return list;
	}

	@Override
	public void oper(String key, Integer type) {
		SysSetting setting = this.getOne(new QueryWrapper<SysSetting>().eq("`key`", key));
		if (setting.getKey().equals(null)) {
			throw new WebException("字典不存在");
		}
		if (type == 2) {
			// 删除
			this.remove(new QueryWrapper<SysSetting>().eq("`key`", key));
		} else if (type == 3) {
			setting.setStatus(setting.getStatus() == 0 ? 1 : 0);
			// 禁用
			this.updateById(setting);
		}
	}

	@Override
	public Result insertOrUpdate(Setting setting) {
		long count = this.count(new QueryWrapper<SysSetting>().eq("`key`", setting.getKey()));
		if (count == 1 && setting.getFlag() == 1) {
			return Result.error("字典已存在");
		}

		SysSetting sysSetting = new SysSetting();
		if (setting.getFlag() == 0) {
			sysSetting = this.getOne(new QueryWrapper<SysSetting>().eq("`key`", setting.getKey()));
		} else {
			sysSetting.setKey(setting.getKey());
		}
		sysSetting.setType(setting.getType() != null ? setting.getType() : 0);
		sysSetting.setStatus(setting.getStatus() == null ? 0 : setting.getStatus());
		sysSetting.setNote(setting.getNote() != null ? setting.getNote() : null);
		sysSetting.setValue(setting.getValue());
		sysSetting.setCreateTime(LocalDateTime.now());
		this.saveOrUpdate(sysSetting);
		return Result.success();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchDelete(List<String> list) {
		list.forEach(item -> {
			this.remove(new QueryWrapper<SysSetting>().eq("`key`", item));
		});
	}

	/**
	 * 获取阿里云OSS相关配置
	 *
	 * @return
	 */
	@Override
	public Map<String, String> getAliOssSetting() {
		List<SysSetting> list = this.list(new QueryWrapper<SysSetting>()
				.in("`key`", Arrays.asList(AliProperties.ACCESS_KEY_SECRET.getValue(),
						AliProperties.BUCKET_NAME.getValue(),
						AliProperties.ENDPOINT.getValue(),
						AliProperties.ACCESS_KEY_ID.getValue(),
						AliProperties.IMG_SERVER.getValue()))
		);

		HashMap<String, String> map = new HashMap<>();
		for (SysSetting setting : list) {
			map.put(setting.getKey(), setting.getValue());
		}
		return map;
	}

	@Override
	public Map<String, AlipayConfig> alipay() {
		List<AlipayConfig> list = configService.list(new QueryWrapper<AlipayConfig>());
		Map<String, AlipayConfig> map = new HashMap<>();
		for (AlipayConfig config : list) {
			map.put("pay" + config.getId(), config);
		}
		return map;
	}


	/**
	 * 保存支付宝支付配置
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(Alipay alipay) {
		this.update(new UpdateWrapper<SysSetting>().eq("`key`", "is_alipay").set("value", alipay.getIsAlipay()));
		AlipayConfig config = new AlipayConfig();
		config.setAppId(alipay.getAppId());
		config.setSignType(alipay.getSignType());
		config.setGatewayHost(alipay.getGatewayHost());
		config.setProtocol(alipay.getProtocol());
		config.setPrivateKey(alipay.getAppPrivateKey());
		config.setPublicKey(alipay.getAppPublicKey());
		config.setNotifyUrl(alipay.getNotifyUrl());
		config.setId(alipay.getId());
		configService.saveOrUpdate(config);
	}

}




